// pages/goodxq/goodxq.js
import { goodxq, unlimit, addshop, goodshoucang, shoucanga, delgood } from "../../http/http";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    good: [],
    show: false,
    num: 0,
    id: 0,
    sfshoucang: false,
    check: false
  },
  // 打开弹出层
  gai() {
    this.setData({
      show: true
    })
  },
  // 关闭弹出层
  gaia() {
    this.setData({
      show: false
    })
  },
  // 收藏
  addshoucang() {
    if (wx.getStorageSync('token')) {
      // 取消收藏
      if (this.data.sfshoucang == true) {
        delgood({ token: wx.getStorageSync('token').token, goodsId: this.data.id }).then(res => {
          wx.showToast({
            title: "取消收藏成功",
            icon: 'success',
            duration: 1000
          })
          this.setData({
            sfshoucang: false
          })
        })
      } else {
        goodshoucang({ token: wx.getStorageSync('token').token, goodsId: this.data.id }).then(res => {
          wx.showToast({
            title: "收藏成功",
            icon: 'success',
            duration: 1000
          })
          this.setData({
            sfshoucang: true
          })
        })
      }
    } else {
      wx.showToast({
        title: "请登录",
        icon: 'success',
        duration: 1000
      })
      wx.redirectTo({
        url: `/pages/shouquan/shouquan?dizhi=${'/pages/goodxq/goodxq'}`
      })
    }

  },
  onClose() {
    this.setData({
      check: false
    })
  },
  Modified(num) {
    console.log(num)
    this.setData({
      num: num.detail
    })
  },
  // 添加购物车
  addshop() {
    if (this.data.num == 0) {
      this.setData({
        num: 1
      })
    }
    addshop({ token: wx.getStorageSync('token').token, goodsId: this.data.id, number: this.data.num }).then(res => {
      console.log(res)
      wx.showToast({
        title: "添加购物车成功",
        icon: 'success',
        duration: 1000
      })
      this.setData({
        show: false
      })
    })

  },
  // 获取二维码
  showPopup() {
    let uid = (wx.getStorageSync('uid'))
    console.log(uid)
    let token = wx.getStorageSync('token')
    unlimit({ scene: uid, token: token }).then(res => {
      console.log(res)
      this.setData({
        url: res.data,
        check: true
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true
    });
    console.log(options)
    this.setData({
      id: options.id
    })
    goodxq({ id: options.id, token: wx.getStorageSync('token').token }).then(res => {
      console.log(res)
      this.setData({
        good: res.data
      })
    })
    // 监听收藏
    shoucanga({ token: wx.getStorageSync('token').token, goodsId: this.data.id }).then(res => {
      console.log(res)
      if (res.data == "已收藏") {
        this.setData({
          sfshoucang: true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})